
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.vo.UserTypeEnum;
import com.insigma.business.qrcode.soldier.entity.CachedObjDO;
import com.insigma.business.qrcode.soldier.service.CacheClient;
import com.insigma.business.qrcode.soldier.service.CachedObjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


@Service("mysqlCacheClient")
public class MysqlCacheClientImpl implements CacheClient {

    @Autowired
    private CachedObjService cachedObjService;

    
    private ConcurrentMap<String, Object> localMap = new ConcurrentHashMap<>();

    private final String TIMEOUT_KEY_POSTFIX = ".timeout";
    private final String CHAR_SPLIT = "_";

    
    /*@Override
    public boolean putWithExpire(final SessionSystemTypeEnum sessionSystemTypeEnum, final String key, final Object value, final Integer expire) {
        final CachedObj cachedObj = new CachedObj();
        cachedObj.setCachedKey(CommonConstant.CACHE_KEY_PREFIX_SMS_TOKEN+key);
        cachedObj.setCachedValue(value.toString());
        cachedObj.setSystemType(sessionSystemTypeEnum.name());
        cachedObj.setTimeoutSecond(expire);
        cachedObj.setRemark();
        cachedObjService.insert()
        *
        localMap.put(key+TIMEOUT_KEY_POSTFIX, expire + CHAR_SPLIT + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));*
        return true;
    }*/

    
    @Override
    public int put(final CachedObjDO cachedObj) {
        final int affectNum = cachedObjService.insert(cachedObj);
        BizCheckUtil.checkIsTrue(affectNum == 1,"新增缓存失败");
        return affectNum;
    }

    
    @Override
    public CachedObjDO get(final String key) {
        return cachedObjService.getByKey(key);
    }

    
    @Override
    public int remove(final String key, final UserTypeEnum userTypeEnum, final String userId) {
        return cachedObjService.deleteByCacheKey(key, userTypeEnum.name(),userId);
    }

}
